#pragma once

#include<QPushButton>
#include<QLabel>
#include "ui_counter.h"
#include<QLayout>
using QS=QString;


class Counter : public QWidget
{
    Q_OBJECT

public:
    Counter(QWidget *parent = nullptr);
    ~Counter();
public:
    void Create()
    {
        increase=new QPushButton("增加",this);
        reduce=new QPushButton("减少",this);
        reduce->setEnabled(false);
        CountLabel=new QLabel("计数器:0次",this);
    }
    void SetLayOut()
    {
        QVBoxLayout* layout=new QVBoxLayout(this);
        layout->addWidget(CountLabel);
        layout->addWidget(increase);
        layout->addWidget(reduce);


    }
public:
    void  setConnect()
    {
        //点击按钮就会增加
        connect(increase,&QPushButton::clicked,[&](){
           //如果计数器是0,这个时候减少的按钮是不可用的
            if(reduce->isEnabled()==false)
            {
                reduce->setEnabled(true);//设置为可用
            }
            //增加count
            ++count;
//            QString num=QString::number(count);
            CountLabel->setText("计数器:"+QS::number(count)+"次");
        });

        //点击按钮就会减少
        connect(reduce,&QPushButton::clicked,[&](){
            --count;
            if(count==0)
            {
                reduce->setEnabled(false);
            }
            CountLabel->setText("计数器:"+QS::number(count)+"次");
        });
    }
private:
    QPushButton* increase;//增加
    QPushButton* reduce;//减少
    QLabel* CountLabel;
    size_t count=0;
    Ui::Widget *ui;
};
